/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.renren.modules.course.controller;

import io.renren.common.utils.Constant;
import io.renren.common.utils.R;
import io.renren.common.utils.ReadExcel;
import io.renren.modules.course.entity.CourseBodyEntity;
import io.renren.modules.course.service.CourseBodyService;
import io.renren.modules.myCourse.entity.ChooseTestEntity;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysDeptEntity;
import io.renren.modules.sys.service.SysDeptService;
import org.apache.commons.collections.map.LinkedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

/**
 * 课程管理
 * 
 * @author zlb
 * @date 2018年7月11日15:42:53
 */
@RestController
@RequestMapping("course/courseTest")
public class CourseTestController extends AbstractController {
	@Autowired
	private SysDeptService sysDeptService;
	@Autowired
	CourseBodyService courseBodyService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("course:coursetest:list")
	public List<SysDeptEntity> list(){
		List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<String, Object>());

		return deptList;
	}

	/**
	 * 选择部门(添加、修改菜单)
	 */
	@RequestMapping("/select")
	@RequiresPermissions("course:coursetest:select")
	public R select(){
		List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<String, Object>());

		//添加一级部门
		if(getUserId() == Constant.SUPER_ADMIN){
			SysDeptEntity root = new SysDeptEntity();
			root.setDeptId(0L);
			root.setName("一级部门");
			root.setParentId(-1L);
			root.setOpen(true);
			deptList.add(root);
		}

		return R.ok().put("deptList", deptList);
	}

	/**
	 * 上级部门Id(管理员则为0)
	 */
	@RequestMapping("/info")
	@RequiresPermissions("course:coursetest:list")
	public R info(){
		long deptId = 0;
		if(getUserId() != Constant.SUPER_ADMIN){
			List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<String, Object>());
			Long parentId = null;
			for(SysDeptEntity sysDeptEntity : deptList){
				if(parentId == null){
					parentId = sysDeptEntity.getParentId();
					continue;
				}

				if(parentId > sysDeptEntity.getParentId().longValue()){
					parentId = sysDeptEntity.getParentId();
				}
			}
			deptId = parentId;
		}

		return R.ok().put("deptId", deptId);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{deptId}")
	@RequiresPermissions("course:coursetest:info")
	public R info(@PathVariable("deptId") Long deptId){
		SysDeptEntity dept = sysDeptService.selectById(deptId);

		return R.ok().put("dept", dept);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("course:coursetest:save")
	public R save(@RequestBody SysDeptEntity dept){
		sysDeptService.insert(dept);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("course:coursetest:update")
	public R update(@RequestBody SysDeptEntity dept){
		sysDeptService.updateById(dept);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("course:coursetest:delete")
	public R delete(long deptId){
		//判断是否有子部门
		List<Long> deptList = sysDeptService.queryDetpIdList(deptId);
		if(deptList.size() > 0){
			return R.error("请先删除子部门");
		}

		sysDeptService.deleteById(deptId);

		return R.ok();
	}

	public ChooseTestEntity getInfoById(int id){
		ChooseTestEntity chooseTestEntity = courseBodyService.getInfoById(id);
		return chooseTestEntity;
	}

	public CourseBodyEntity getCourseBodyById(int id){
		CourseBodyEntity courseBodyEntity = courseBodyService.getCourseBodyById(id);
		return courseBodyEntity;
	}


	public int getCourseIdByCourseBodyId(int courseBodyId){
		int courseId = courseBodyService.getCourseIdByCourseBodyId(courseBodyId);
		return  courseId;
	}

	@GetMapping("deleteCourseTest")
	public R deleteCourseTest( String searching){
		if(!"".equals(searching)& null != searching & !"null".equals(searching) & !"undefined".equals(searching)){
			courseBodyService.deleteCourseTest(Integer.parseInt(searching));
			return R.ok();
		}
		return R.error(101,"请选择需要删除的课程");
	}


	/**
	 * 上传文件
	 */
	@RequestMapping(value="/uploadTest",method = RequestMethod.POST)
	@ResponseBody
	//@RequiresPermissions("course:coursetest:uploadTest")
	public R uploadTest(@RequestParam(value="file",required = false) MultipartFile file, HttpServletRequest request) {
		return courseBodyService.uploadTests(file);
	}


}
